<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/displaytag.tld" prefix="display"%>
<%@ page import="java.util.*"%>

<script type="text/javascript"
	src="<%= request.getContextPath() + "/js/popUP.js" %>"></script>
<link href="<%= request.getContextPath() + "/css/bloques.css" %>"
	rel="stylesheet" type="text/css">
<link href="<%= request.getContextPath() + "/css/table.css" %>"
	rel="stylesheet" type="text/css">


<script type="text/javascript">
  function buscarSubprogramas(accion,destino){
    habilita2();
    document.formBeneficiosSede.target = destino;
    document.formBeneficiosSede.action = '<%=request.getContextPath()+"/Reportes/Totales/ReportTotalComuna.do"%>';
    document.formBeneficiosSede.acciones.value = accion;
   
    document.formBeneficiosSede.submit();
  }
    
  function validarCantidad(field){
    var valid = "0123456789";
    var ok = "yes";
    var temp;
    for (var i=0; i<field.value.length; i++) {
    temp = "" + field.value.substring(i, i+1);
    if (valid.indexOf(temp) == "-1") ok = "no";
    }
    if (ok == "no") {
    alert("Cantidad no valido, solo se permiten numeros!!");
    field.focus();
    field.select();
    }
  }
  
  function habilita(nombre){
     document.forms['formBeneficiosSede'].elements[nombre].disabled=false;
  }
  
  function habilita2(){
     document.formBeneficiosSede.mes1.disabled = false; 
     document.formBeneficiosSede.mes2.disabled = false;
     document.formBeneficiosSede.ano1.disabled = false;
     document.formBeneficiosSede.ano2.disabled = false;
    
  } 

 var a, mes, dia, anyo, febrero;
  
 function anyoBisiesto(anyo)
 {
	 var fin;
     if (anyo < 100)
         fin = anyo + 1900;
     else
         fin = anyo ;
     if (fin % 4 != 0)
         return false;
     else
     {
         if (fin % 100 == 0)
         {
             if (fin % 400 == 0)
             {
                 return true;
             } else
             {
                 return false;
             }
         }
         else
         {
             return true;
         }
     }
 }
    


 function validar(vdia,vmes,vano)
 {
    var d = document.forms['formBeneficiosSede'].elements[vdia].value;
    var m = document.forms['formBeneficiosSede'].elements[vmes].value;
    var an = document.forms['formBeneficiosSede'].elements[vano].value;
    a=d+"/"+m+"/"+an;
    dia=a.split("/")[0];
    mes=a.split("/")[1];
    anyo=a.split("/")[2];
    if( (isNaN(dia)==true) || (isNaN(mes)==true) || (isNaN(anyo)==true) )
    {
       alert("La fecha introducida debe estar formada solo por numeros");
       return;
    }
    if(anyoBisiesto(anyo))
        febrero=29;
    else
        febrero=28;
      
    if ((mes<1) || (mes>12))
    {
        alert("El mes introducido no es valido. Por favor, introduzca un mes correcto");
        document.forms['formBeneficiosSede'].elements[vmes].focus();
        document.forms['formBeneficiosSede'].elements[vdia].value='nulo';
        document.forms['formBeneficiosSede'].elements[vmes].value='nulo';
        document.forms['formBeneficiosSede'].elements[vmes].disabled=true;
        document.forms['formBeneficiosSede'].elements[vano].value='nulo';
        document.forms['formBeneficiosSede'].elements[vano].disabled=true;
        //document.actualizarBeneficiario.fecha.select();
        return;
    }
       
    if ((mes==2) && ((dia<1) || (dia>febrero)))
    {
        alert("El dia introducido no es valido. Por favor, introduzca un dia correcto");
        
        document.forms['formBeneficiosSede'].elements[vdia].focus();
        document.forms['formBeneficiosSede'].elements[vdia].value='nulo';
        document.forms['formBeneficiosSede'].elements[vmes].value='nulo';
        document.forms['formBeneficiosSede'].elements[vmes].disabled=true;
        document.forms['formBeneficiosSede'].elements[vano].value='nulo';
        document.forms['formBeneficiosSede'].elements[vano].disabled=true;
        //document.actualizarBeneficiario.fecha.select();
        return;
    }
      
    if (((mes==1) || (mes==3) || (mes==5) || (mes==7) || (mes==8) || (mes==10) || (mes==12)) && ((dia<1) || (dia>31)))
    {
        alert("El dia introducido no es valido. Por favor, introduzca un dia correcto");
        
        document.forms['formBeneficiosSede'].elements[vdia].focus();
        document.forms['formBeneficiosSede'].elements[vdia].value='nulo';
        document.forms['formBeneficiosSede'].elements[vmes].value='nulo';
        document.forms['formBeneficiosSede'].elements[vmes].disabled=true;
        document.forms['formBeneficiosSede'].elements[vano].value='nulo';
        document.forms['formBeneficiosSede'].elements[vano].disabled=true;
        //document.actualizarBeneficiario.fecha.select();
        return;
    }
       
    if (((mes==4) || (mes==6) || (mes==9) || (mes==11)) && ((dia<1) || (dia>30)))
    {
        alert("El dia introducido no es valido. Por favor, introduzca un dia correcto");
        
        document.forms['formBeneficiosSede'].elements[vmes].focus();
        document.forms['formBeneficiosSede'].elements[vdia].value='nulo';
        document.forms['formBeneficiosSede'].elements[vmes].value='nulo';
        document.forms['formBeneficiosSede'].elements[vmes].disabled=true;
        document.forms['formBeneficiosSede'].elements[vano].value='nulo';
        document.forms['formBeneficiosSede'].elements[vano].disabled=true;
        //document.actualizarBeneficiario.fecha.select();
        return;
    }
       
    if ((anyo<1990) || (anyo>2040))
    {
       alert("El año introducido no es valido. Por favor, introduzca un año entre 1900 y 2010");
       
       document.forms['formBeneficiosSede'].elements[vano].focus();
       document.forms['formBeneficiosSede'].elements[vdia].value='nulo';
       document.forms['formBeneficiosSede'].elements[vmes].value='nulo';
       document.forms['formBeneficiosSede'].elements[vmes].disabled=true;
       document.forms['formBeneficiosSede'].elements[vano].value='nulo';
       document.forms['formBeneficiosSede'].elements[vano].disabled=true;
       //document.actualizarBeneficiario.fecha.select();
       return;
    }
 }
 
   
 function Sede(){ 
    document.formBeneficiosSede.submit();
 }
 
</script>
<script src="<%=request.getContextPath()%>/js/displaytag.js"
	type="text/javascript"></script>
<form name="formBeneficiosSede"
	action="<%=request.getContextPath()+"/Reportes/Totales/ReportTotalBOT.do"%>"
	method="get">
	<input type="HIDDEN" name="seleccionar" value="ir"> <input
		type="HIDDEN" name="codigo" value="nulo"> <input type="HIDDEN"
		name="acciones"> <input type="HIDDEN" name="posicion" />

	<div align="center">
		<table class="tablaGenericaWidth70">
			<tr>
				<td class="titulosenblanco" colspan="3">
					<DIV align="center">
						<STRONG>Reporte&nbsp;Total&nbsp;Beneficiarios&nbsp;y&nbsp;Beneficios
							por Comuna&nbsp;-&nbsp;Corregimiento </STRONG>
					</DIV>
				</td>
			</tr>
			<tr>
				<td class="titulosenblanco" colspan="3">&nbsp;</td>
			</tr>
			<tr>
				<td align="center" class="celdatabla" colspan="3">Seleccione
					los criterios de busqueda</td>
			</tr>
			<tr>
				<td class="titulosenblanco" colspan="3">&nbsp;</td>
			</tr>
			<tr>
				<td class="celdatabla" width="31%">Comuna - Corregimiento:</td>
				<td width="44%"><logic:notPresent name="comunas">
						<DIV align="center">
							<%= session.getAttribute("msg")%>
							<%session.removeAttribute("msg");%>
						</DIV>
					</logic:notPresent> <%   Object codeComuna = request.getParameter("code"); 
            if(codeComuna==null) codeComuna = "nulo";
       %>
					<div align="left">
						<logic:present name="comunas">
							<logic:iterate id="comuna" name="comunas" length="1"
								type="com.cs.beneficios.business.model.VflexVO">
								<html:select name="comuna" property="code" title="Comuna"
									styleClass="form" value="<%=(String)codeComuna%>">
									<option value="nulo" selected="selected">Seleccione
										una comuna</option>
									<html:options name="comuna" labelName="codeComuna"
										labelProperty="descVflex" property="code" collection="comunas" />
								</html:select>
							</logic:iterate>
						</logic:present>
					</div></td>
				<td class="celdatabla" width="25%">&nbsp;</td>
			<tr>
				<td class="celdatabla" width="31%">Programa:</td>
				<td width="44%"><logic:notPresent name="programas">
						<DIV align="center">
							<%= session.getAttribute("msg")%>
							<%session.removeAttribute("msg");%>
						</DIV>
					</logic:notPresent> <%   Object codePrograma = request.getParameter("codePrograma"); %>
					<div align="left">
						<logic:present name="programas">
							<logic:iterate id="programa" name="programas" length="1"
								type="com.cs.beneficios.business.model.ProgramaVO">
								<html:select name="programa" property="codePrograma"
									onchange="buscarSubprogramas('BuscarSubprogrmas','_self');"
									title="Programa" styleClass="form"
									value="<%=(String)codePrograma%>">
									<option value="nulo">Seleccione un programa</option>
									<html:options name="programas" labelName="codigo"
										labelProperty="nombre" property="code" collection="programas" />
								</html:select>
							</logic:iterate>
						</logic:present>
					</div></td>
				<td class="celdatabla" width="25%">&nbsp;</td>
			</tr>
			<% 
        if(codePrograma != null){
        %>
			<tr>
				<td class="celdatabla" width="31%">Beneficio:</td>
				<td width="44%">
					<div align="left">
						<logic:notPresent name="subprogramas">
							<DIV align="center">
								<%= session.getAttribute("msg")%>
								<%session.removeAttribute("msg");%>
							</DIV>
						</logic:notPresent>
						<%   Object codeSubPrograma = request.getParameter("codeSubPrograma"); 
             if(codeSubPrograma==null) codeSubPrograma = "nulo";
       %>
						<logic:present name="subprogramas">
							<logic:iterate id="subprograma" name="subprogramas" length="1"
								type="com.cs.beneficios.business.model.SubProgramaVO">
								<html:select name="subprograma" property="codeSubPrograma"
									title="Programa" styleClass="form"
									value="<%=(String)codeSubPrograma%>">
									<option value="nulo">Seleccione un subprograma</option>
									<html:options name="subprogramas" labelName="codigo"
										labelProperty="nombre" property="code"
										collection="subprogramas" />
								</html:select>
							</logic:iterate>
						</logic:present>
					</div>
				</td>
				<td class="celdatabla" width="25%">&nbsp;</td>
			</tr>
			<% 
      }  
  %>

			<tr>
				<td class="celdatabla" width="31%">Fecha inicial:</td>
				<td align="left"><FONT class="celdatabla">dia</FONT> <select
					name="dia1" onclick="habilita('mes1')" Class="form">
						<option value="nulo">---</option>
						<%
							String diaSel1 = request.getParameter("dia1") == null
									? ""
									: request.getParameter("dia1");
							for (int d = 1; d <= 31; d++) {
								String dia = String.valueOf(d);
								if (d < 10)
									dia = "0" + d;
								if (dia.equals(diaSel1)) {
						%>
						<option class="form" value="<%=dia%>" selected><%=dia%></option>
						<%
							} else {
						%>
						<option class="form" value="<%=dia%>"><%=dia%></option>
						<%
							}
							}
						%>

				</select> <FONT class="celdatabla">mes</FONT> <select name="mes1"
					onchange="habilita('ano1')" Class="form">
						<option value="nulo">---</option>
						<%
							String mesSel1 = request.getParameter("mes1") == null
									? ""
									: request.getParameter("mes1");
							for (int m = 1; m <= 12; m++) {
								String mes = String.valueOf(m);
								if (m < 10)
									mes = "0" + m;
								if (mes.equals(mesSel1)) {
						%>
						<option class="form" value="<%=mes%>" selected><%=mes%></option>
						<%
							} else {
						%>
						<option class="form" value="<%=mes%>"><%=mes%></option>
						<%
							}
							}
						%>

				</select> <FONT class="celdatabla">a&ntilde;o</FONT> <select name="ano1"
					onchange="validar('dia1','mes1','ano1')" Class="form">
						<option value="nulo">-----</option>
						<%
							String ano1 = request.getParameter("ano1") == null ? "" : request
									.getParameter("ano1");
							for (int a = 1990; a <= 2040; a++) {
								String ano = String.valueOf(a);
								if (ano.equals(ano1)) {
						%>
						<option class="form" value="<%=ano%>" selected><%=ano%></option>
						<%
							} else {
						%>
						<option class="form" value="<%=ano%>"><%=ano%></option>
						<%
							}
							}
						%>
				</select></td>


				<td class="celdatabla" width="25%">&nbsp;</td>
			</tr>
			<tr>
				<td class="celdatabla" width="31%">Fecha final:</td>
				<td align="left"><FONT class="celdatabla">dia</FONT> <select
					name="dia2" onchange="habilita('mes2')" Class="form">
						<option value="nulo">---</option>
						<%
							String diaSel2 = request.getParameter("dia2") == null
									? ""
									: request.getParameter("dia2");
							for (int d = 1; d <= 31; d++) {
								String dia = String.valueOf(d);
								if (d < 10)
									dia = "0" + d;
								if (dia.equals(diaSel2)) {
						%>
						<option class="form" value="<%=dia%>" selected><%=dia%></option>
						<%
							} else {
						%>
						<option class="form" value="<%=dia%>"><%=dia%></option>
						<%
							}
							}
						%>

				</select> <FONT class="celdatabla">mes</FONT> <select name="mes2"
					onchange="habilita('ano2')" Class="form">
						<option value="nulo">---</option>
						<%
							String mesSel2 = request.getParameter("mes2") == null
									? ""
									: request.getParameter("mes2");
							for (int m = 1; m <= 12; m++) {
								String mes = String.valueOf(m);
								if (m < 10)
									mes = "0" + m;
								if (mes.equals(mesSel2)) {
						%>
						<option class="form" value="<%=mes%>" selected><%=mes%></option>
						<%
							} else {
						%>
						<option class="form" value="<%=mes%>"><%=mes%></option>
						<%
							}
							}
						%>

				</select> <FONT class="celdatabla">a&ntilde;o</FONT> <select name="ano2"
					onchange="validar('dia2','mes2','ano2')" Class="form">
						<option value="nulo">-----</option>

						<%
							String ano2 = request.getParameter("ano2") == null ? "" : request
									.getParameter("ano2");
							for (int a = 1990; a <= 2040; a++) {
								String ano = String.valueOf(a);
								if (ano.equals(ano2)) {
						%>
						<option class="form" value="<%=ano%>" selected><%=ano%></option>
						<%
							} else {
						%>
						<option class="form" value="<%=ano%>"><%=ano%></option>
						<%
							}
							}
						%>

				</select></td>

				<td class="celdatabla" width="25%">&nbsp;</td>
			</tr>
			<tr>
				<td class="celdatabla" width="31%">&nbsp;</td>
				<td width="44%">
					<div align="center">
						<input type="hidden" name="accion" value="" /> <input
							type="button" value="Generar" name="buscar"
							onclick="buscarSubprogramas('BuscarTodo','blank');" />
						<P>&nbsp;</P>
					</div>
				</td>
				<td class="celdatabla" width="25%">&nbsp;</td>
			</tr>
			<tr>
				<td colspan="3" align="center"><logic:present name="msg">
						<DIV align="center"><%=session.getAttribute("msg")%></DIV>
						<%session.removeAttribute("msg");%>
					</logic:present></td>
			</tr>
		</table>
	</div>
</form>